home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / Document < prev    next >
Text File  |  2003-02-14  |  6KB  |  219 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "Document.h"
  39. #include "DocView.h"
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include "GuiHourglass.h"
  43. #include "strstream.h"
  44. #include "error.h"
  45. #include "Page.h"
  46. #include "link.h"
  47. #include "Stream.h"
  48. #include "fstream.h"
  49. #include "file.h"
  50. #include "password.h"
  51. #include "Outline.h"
  52. #include "ErrorCodes.h"
  53.  
  54. EraseScrapFile::~EraseScrapFile()
  55. {
  56.   int pos=filename.find("!Scrap.ScrapDirs.ScrapDir.");
  57.   if (pos != string::npos)
  58.   {
  59.     if (filename.find('.',pos+26)==string::npos) remove(filename.c_str());
  60.   }
  61. }
  62.  
  63. //*************************************************************************
  64. //*************************************************************************
  65. //*************************************************************************
  66.  
  67. DEFINE_RTTI_DERIVED(Document,Node);
  68.  
  69. bool Document::hasChildNodes() {return 1;}
  70.  
  71. Document* makeDocument(Node& application,const char* file_name)
  72. {
  73.   if (file_name)
  74.   {
  75.     Node* n;
  76.     for (n=application.getFirstChild();n;n=n->getNextSibling())
  77.     {
  78.       Document* doc=PTR_dynamic_cast(Document,n);
  79.       if (doc && doc->fileName==file_name)
  80.       {
  81.         return doc;
  82.       }
  83.     }
  84.  
  85.     ifstream in(file_name);
  86.     if (!in)
  87.     {
  88.       error(0,"Couldn't open file '%s'",file_name);
  89.       return 0;
  90.     }
  91.     in.close();
  92.  
  93.     Document* doc=new Document(application,file_name);
  94.     if (doc && doc->isOk()) return doc;
  95.     delete doc;
  96.   }
  97.   return 0;
  98. }
  99.  
  100. //*************************************************************************
  101.  
  102.  
  103. Document::Document(Node& app,const char* file_name)
  104.  : fileName(file_name),
  105.    eraseScrapFile(file_name),
  106.    doc(0),
  107.    outline(0)
  108. {
  109.   {
  110.     GuiHourglass hourglass;
  111.     doc=new PDFDoc(new GString(file_name));
  112.   }
  113.   if (!doc)  return;
  114.   
  115.   if (doc->getErrorCode() == errEncrypted)
  116.   {
  117.     delete doc;
  118.     doc=0;
  119.  
  120.     int i;
  121.     for (i=0;i<3;i++)
  122.     {
  123.       string user,owner;
  124.  
  125.       if (!getPassword(user,owner)) break;
  126.  
  127.       {
  128.         GuiHourglass hourglass;
  129.         doc=new PDFDoc(new GString(fileName.c_str()),
  130.                        new GString((char*)owner.c_str()),
  131.                        new GString((char*)user.c_str()));
  132.       }
  133.       if (doc && !isOk()) {delete doc;doc=0;}
  134.       if (isOk()) break;
  135.     }
  136.   }
  137.  
  138.   if (isOk())
  139.   {
  140.     outline=new Outline(getPDFDoc());
  141.     FileInfo_create(fileInfoData,getPDFDoc(),fileName);
  142.     app.addChild(this);
  143.   }
  144. }
  145.  
  146. //*************************************************************************
  147.  
  148. Document::~Document()
  149. {
  150.   delete outline;
  151.   delete doc;
  152. }
  153.  
  154. //*************************************************************************
  155.  
  156. Node *Document::removeChild(Node* n)
  157. {
  158.   if (!Node::removeChild(n)) return 0;
  159.   if (getFirstChild()==0)
  160.     delete this;
  161.   else 
  162.     if (PTR_dynamic_cast(DocView,n)) setViewTitles();
  163.   return n;
  164. }
  165.  
  166. //*************************************************************************
  167.  
  168. Node *Document::addChild(Node* n)
  169. {
  170.   if (!Node::addChild(n)) return 0;
  171.   if (PTR_dynamic_cast(DocView,n)) setViewTitles();
  172.   return n;
  173. }
  174.  
  175. //*************************************************************************
  176.  
  177. void Document::setViewTitles()
  178. {
  179.   int count=0;
  180.   Node* n;
  181.   for (n=getFirstChild();n;n=n->getNextSibling())
  182.   {
  183.     if (PTR_dynamic_cast(DocView,n)) count++;
  184.   }
  185.  
  186.   if (!count) return;
  187.  
  188.   char buf[256];
  189.   ostrstream out(buf,sizeof(buf));
  190.   out << (fileInfoData.title.size() ? fileInfoData.title : fileName);
  191.   if (count>1) out << " - " << count;
  192.  
  193.   DocView* view;
  194.   for (n=getFirstChild();n;n=n->getNextSibling())
  195.   {
  196.     view=PTR_dynamic_cast(DocView,n);
  197.     if (view) view->setTitle(out.str());
  198.   }
  199. }
  200.  
  201. //*************************************************************************
  202.  
  203. void Document::getPage(int num,OutputDev* out,int dpi,int rotate)
  204. {
  205.   getPDFDoc().displayPage(out,num,(double)dpi,rotate,0);
  206. }
  207.  
  208. //*************************************************************************
  209.  
  210. Links* Document::makeLinks(int page)
  211. {
  212.   Object obj;
  213.   Links* links=new Links(getCatalog()->getPage(page)->getAnnots(&obj),
  214.                          getCatalog()->getBaseURI());
  215.   obj.free();
  216.   return links;
  217. }
  218.  
  219.